#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

function preConfig()
{
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
echo "1" > /proc/sys/net/ipv4/ip_forward
# 关闭交换分区
swapoff -a
sed -i /swap/d /etc/fstab
}

function onlineInstall()
{
# 选择要下载的版本
k8s_version=${1:-"v1.20.0"}

# 根据版本下载镜像
if [ "$k8s_version" == "v1.27.2" ]; then
  # 下载1.27.2版本镜像
  echo "正在下载 Kubernetes v1.27.2 镜像..."
  ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.27.2
  ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.27.2
  ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.27.2
  ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.27.2
  ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9
  ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.7-0
  ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.10.1

  ctr -n k8s.io image tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.27.2 registry.k8s.io/kube-apiserver:v1.27.2
  ctr -n k8s.io image tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.27.2 registry.k8s.io/kube-controller-manager:v1.27.2
  ctr -n k8s.io image tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.27.2 registry.k8s.io/kube-scheduler:v1.27.2
  ctr -n k8s.io image tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.27.2 registry.k8s.io/kube-proxy:v1.27.2
  ctr -n k8s.io image tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9 registry.k8s.io/pause:3.9
  ctr -n k8s.io image tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.7-0 registry.k8s.io/etcd:3.5.7-0
  ctr -n k8s.io image tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.10.1 registry.k8s.io/coredns/coredns:v1.10.1

  # 额外下载和重命名 pause:3.6 镜像
  echo "正在下载和重命名 pause:3.6 镜像..."
  ctr -n k8s.io image pull registry.aliyuncs.com/google_containers/pause:3.6
  ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/pause:3.6 registry.k8s.io/pause:3.6

elif [ "$k8s_version" == "v1.20.1" ]; then
  # 下载1.20.1版本镜像
  echo "正在下载 Kubernetes v1.20.1 镜像..."
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.1
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.1
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.1
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.1
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0

  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.1 k8s.gcr.io/kube-apiserver:v1.20.1
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.1 k8s.gcr.io/kube-controller-manager:v1.20.1
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.1 k8s.gcr.io/kube-scheduler:v1.20.1
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.1 k8s.gcr.io/kube-proxy:v1.20.1
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0
else
  # 下载1.20.0版本镜像（默认）
  echo "正在下载 Kubernetes v1.20.0 镜像..."
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.0
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.0
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.0
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.0
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
  docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0

  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.0 k8s.gcr.io/kube-apiserver:v1.20.0
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.0 k8s.gcr.io/kube-controller-manager:v1.20.0
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.0 k8s.gcr.io/kube-scheduler:v1.20.0
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.0 k8s.gcr.io/kube-proxy:v1.20.0
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0
  docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0
fi

# 添加阿里云仓库
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 安装kubeadm组件
if [ "$k8s_version" == "v1.27.2" ]; then
  echo "正在安装 Kubernetes v1.27.2 组件..."
  yum install -y kubelet-1.27.2 kubeadm-1.27.2 kubectl-1.27.2 --disableexcludes=kubernetes
elif [ "$k8s_version" == "v1.20.1" ]; then
  echo "正在安装 Kubernetes v1.20.1 组件..."
  yum install -y kubelet-1.20.1 kubeadm-1.20.1 kubectl-1.20.1 --disableexcludes=kubernetes
else
  echo "正在安装 Kubernetes v1.20.0 组件..."
  yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0 --disableexcludes=kubernetes
fi

systemctl enable --now kubelet

kubeadm version
kubectl version
}

function installK8s()
{
 #1.在frames.txt中查看是否需要安装flink
 k8sInfo=`egrep "^k8s" $PLAY_HOME/frames.txt`

 k8s=`echo $k8sInfo | cut -d " " -f1`
 isInstall=`echo $k8sInfo | cut -d " " -f2`
 k8sNodes=`echo $k8sInfo | cut -d " " -f3` 
 masterNode=`echo $k8sInfo | cut -d " " -f4` 
 node=`hostname`

 masterInfo=`egrep "$masterNode" $PLAY_HOME/host_ip.txt`
 masterIP=`echo $masterInfo | cut -d " " -f1`

 #是否安装
 if [[ $isInstall = "true" && $k8sNodes =~ $node ]];then
    
     # 对节点进行配置 
     preConfig
     
     #2.查看/opt/frames目录下是否有k8s安装包
     k8sIsExists=`find /opt/frames -name $k8s`

     if [[ ${#k8sIsExists} -ne 0 ]];then
        # 离线方式安装
        echo "离线方式安装K8S"
     else
         echo "/opt/frames目录下没有k8s安装包，开始在线安装"
         # 传递版本参数给onlineInstall函数
         if [ -n "$1" ]; then
           onlineInstall "$1"
         else
           onlineInstall "v1.20.0"
         fi
     fi
 else
     echo "K8S不允许安装在当前节点，请检查配置文件！"
 fi
}

# 如果有命令行参数，则传递给installK8s函数
if [ -n "$1" ]; then
  installK8s "$1"
else
  installK8s
fi
